import JsonData from "../../Json.js";
let { brideName, groomName, groomAvatar, wedding, sharePhoto, location } = JsonData.invite;
let chatList = JsonData.chatList;
let app = getApp();
let plugin = requirePlugin('routePlan');
let key = 'QEUBZ-CUFYX-APJ4P-TNQOW-KFEQV-BIBIS';  //使用在腾讯位置服务申请的key
let referer = 'wx50b5593e81dd937a';   //调用插件的app的名称
let endPoint = JSON.stringify({  //终点
  name: location.address,
  latitude: location.latitude,
  longitude: location.longitude,
});

Page({
  data: {
    address: location.address,
    receivedTime: "",
    time: "",
    userInfo: {},
    prophoto: groomAvatar,
    wedding: wedding,
    chatList: [],
    scrolltop: "",
    askWord: "",
    sendButtDisable: true,
    sendDisplay: "none",
    plusDisplay: "block",
    motto: "getting marry",
    // index: 1,
    markers: [{
      title: location.address,
      iconPath: "/images/heart.gif",
      id: 0,
      latitude: 29.51472859,
      longitude: 116.49434567,
      anchor: {
        x: .8,
        y: .8
      },
      width: 50,
      height: 50
    }],
    scale: 8
  },
  onLoad: function(options) {
    this.setData({
      receivedTime: options.receivedTime,
      userInfo: app.globalData.userInfo
    });
    this.mapCtx = wx.createMapContext("myMap");
  },
  onReady: function() {
    wx.vibrateLong();
    let than = this;
    let index = 0;
    this.data.timer = setInterval(function () {
      index++ ;
      app.globalData.index++ ;
      if(index<16){
        switch (index) {
          case 1:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 2:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 3:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 4:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 5:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 6:
            than.setData({
              scale: 10
            })
            break
          case 7:
            than.setData({
              scale: 11
            })
            break
          case 8:
            than.setData({
              scale: 16
            })
            break
          case 9:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 10:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 11:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 12:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 13:
            than.addChat(chatList[index].text, chatList[index].orientation);
            break
          case 14:
            than.addChat(chatList[index].text, chatList[index].orientation);
            than.addChat(chatList[15].text, chatList[15].orientation);
          case 15:
           
            break
          default:
            break
        }
      }else{
        clearInterval(than.data.timer);
      }
    }, 1000);
  },
  addChat: function (text, orientation, index) {
    let info = {
      text: text,
      time: new Date().getTime(),
      orientation: orientation,
      index: index
    };
    let chatList= this.data.chatList;
    chatList.push(info);
    let idx = chatList.length;
    this.setData({
      chatList: chatList,
      scrolltop: "roll" + idx
    });
  },
  markertap: function (e) {
    console.log(e.markerId);
  },
  bindViewTap: function () {
    wx.navigateTo({
      url: "../gallery/gallery"
    });
  },
  toVideoPage: function () {
    wx.navigateTo({
      url: "../video/video"
    });
  },
  bindNavi: function () {
    wx.navigateTo({
       url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    });
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function (res) {
        console.log(res.longitude);
        console.log(res.latitude);
      }
    });
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation();
  },
  translateMarker: function () {
    this.mapCtx.translateMarker({
      markerId: 1,
      autoRotate: !0,
      duration: 1e3,
      destination: {
        latitude: location.latitude,
        longitude: location.longitude
      },
      animationEnd: function () {
        console.log("animation end");
      }
    });
  },
  includePoints: function () {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude: location.latitude,
        longitude: location.longitude
      }, {
          latitude: location.latitude,
          longitude: location.longitude
      }]
    });
  },
  Typing: function (e) {
    this.setData({
      sendButtDisable: e.detail.value.length > 0?false:true,
      sendDisplay: "block",
      plusDisplay: "none"
    });
  },
  sendChat: function (e) {
    let askWord= e.detail.value.ask_word ? e.detail.value.ask_word : e.detail.value;
    this.addChat(askWord, "r", app.globalData.index++);
    this.setData({
      askWord: "",
      sendButtDisable: true,
      sendDisplay: "none",
      plusDisplay: "block"
    });
  },
  onShow: function() {},
  onHide: function() {},
  onUnload: function() {
    clearInterval(this.data.timer);
  },
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  onShareAppMessage: function (e) {
    return "button" === e.from && console.log(e.target), {
      title: `${brideName}&${groomName}的结婚邀请函`,
      path: "pages/index/index",
      imageUrl: sharePhoto,
      success: function (res) {
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (err) {
        console.log("转发失败:" + JSON.stringify(err));
      }
    };
  }
});